<?php include_once( "_db.php" ); //Database ?>
<?php include_once( "funcdef.php" ); //Functions Definition ?>
<?php
//TMP DEFINES
define("SHOPNAME", "Schanaz");
define("SHOPEMAILTITLE", "Billing Status");
//db OBJECT invoked!
$db = new DATABASE();
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title><?php echo SHOPNAME." ".SHOPEMAILTITLE; ?></title>
  </head>
  <body>
<?php
if (isset($_GET["dealid"]) && isset($_GET["email"])) {
    //Get Deal
    $cols = NULL;
    $conds = array();
    $conds[] = array(0);
    $conds[] = array(0);
    $conds[] = array($_GET["dealid"]);
    $query = $db->db_select("deal", $cols, $conds);
    $queries[] .= $query.";";
    $result = $db->database->query($query);
    $tmpDeal = $result->fetchAll(SQLITE_ASSOC);

    if (!empty($tmpDeal)) {

      //Get Customer
      $cols = NULL;
      $conds = array();
      $conds[] = array(0);
      $conds[] = array(0);
      $conds[] = array($tmpDeal[0]['customer_id']);
      $query = $db->db_select("customer", $cols, $conds);
      $queries[] .= $query.";";
      $result = $db->database->query($query);
      $tmpCust = $result->fetchAll(SQLITE_ASSOC);

      if ((!empty($tmpCust)) && ($_GET["email"] == $tmpCust[0]['customer_email'])) {

        //Get Address
        $cols = NULL;
        $conds = array();
        $conds[] = array(0);
        $conds[] = array(0);
        $conds[] = array($tmpDeal[0]['address_id']);
        $query = $db->db_select("address", $cols, $conds);
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpAddr = $result->fetchAll(SQLITE_ASSOC);

        //Get Postage
        $cols = NULL;
        $conds = array();
        $conds[] = array(1);
        $conds[] = array(0);
        $conds[] = array($tmpAddr[0]['address_country']);
        $query = $db->db_select("postage", $cols, $conds);
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpPostg = $result->fetchAll(SQLITE_ASSOC);

        //Get Product2Deal
        $cols = NULL;
        $conds = array();
        $conds[] = array(2);
        $conds[] = array(0);
        $conds[] = array($tmpDeal[0]['deal_id']);
        $query = $db->db_select("product2deal", $cols, $conds);
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpProd2Deal = $result->fetchAll(SQLITE_ASSOC);

        //Construct Temp BAG
        $prodIds = array();
        foreach ($tmpProd2Deal as $value) {
          $prodIds[] = $value["product_id"];
        }
        sort($prodIds);
        $prodIdsUnique = array_unique($prodIds);
        $prodIdsCount = array_count_values($prodIds);

        //Construct Temp PRODUCT
        $prodIdsUniqueStr = implode(",", $prodIdsUnique);
        $query = "SELECT * FROM product WHERE product_id in (".$prodIdsUniqueStr.")";
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpProd = $result->fetchAll(SQLITE_ASSOC);

        //Get Bill
        $cols = NULL;
        $conds = array();
        $conds[] = array(15);
        $conds[] = array(0);
        $conds[] = array($tmpDeal[0]['deal_id']);
        $query = $db->db_select("bill", $cols, $conds);
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpBill = $result->fetchAll(SQLITE_ASSOC);
?>
    <table cellpadding="0" cellspacing="0" border="0" width="100%" style="background-color:#dddddd;">
      <tbody>
        <tr>
          <td>
            <table align="center" cellpadding="0" cellspacing="0" border="0" width="670" style="border:0px;border-left:1px solid #cccccc;border-right:1px solid #cccccc;background-color:#ffffff;">
              <tbody>
                <tr>
                  <td>
                    <table cellpadding="20" cellspacing="0" border="0" width="670" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#f2f2f2;table-layout:fixed;">
                      <tbody>
                        <tr>
                          <td>
                            <table align="center" cellpadding="0" cellspacing="0" width="520" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;">
                              <tbody>
                                <tr>
                                  <td colspan="3"style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-bottom:1px solid #dddddd;">
                                    <h3><?php echo SHOPNAME; ?> <small><?php echo SHOPEMAILTITLE; ?></small></h3>
                                  </td>
                                </tr>
                                <tr>
                                  <td width="175">
                                    <small><strong>Number:</strong><br /><?php echo $tmpBill[0]['bill_mol_oderid']; ?></small>
                                  </td>
                                  <td width="170">&nbsp;</td>
                                  <td width="175">
                                    <small><strong>Date:</strong><br /><?php echo date("Y-m-d H:i:s", $tmpBill[0]['bill_timestamp']); ?></small> <a title="(UTC+08:00) Kuala Lumpur, Singapore">&otimes;</a>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                      </tbody>
                    </table>
                  </td>
                </tr>
                <tr>
                  <td>
                    <table cellpadding="20" cellspacing="0" border="0" width="670" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#ffffff;table-layout:fixed;">
                      <tbody>
                        <tr>
                          <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;">
                            <table align="center" cellpadding="0" cellspacing="0" width="520" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;">
                              <tbody>
                                <tr>
                                  <td>
                                    <?php if ($tmpBill[0]['bill_status'] == "completed") { ?>
                                    <table cellpadding="3" cellspacing="0" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;">
                                      <tbody>
                                        <tr>
                                          <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#46a546;color:#ffffff;">
                                            <small>ORDER PAID</small>
                                          </td>
                                        </tr>
                                      </tbody>
                                    </table>
                                    <?php } else { ?>
                                    <table cellpadding="3" cellspacing="0" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;">
                                      <tbody>
                                        <tr>
                                          <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#9d261d;color:#ffffff;">
                                            <small>ORDER UNPAID</small>
                                          </td>
                                        </tr>
                                      </tbody>
                                    </table>
                                    <?php } ?>
                                  </td>
                                </tr>
                                <tr>
                                  <td width="175" style="vertical-align:top;">
                                    <h4>BILL TO:</h4>
                                    <small>
                                    <strong><?php echo $tmpAddr[0]['address_fullname']; ?></strong><br />
                                    <?php echo $tmpCust[0]['customer_email']; ?><br />
                                    </small>
                                  </td>
                                  <td width="170" style="vertical-align:top;">&nbsp;</td>
                                  <td width="175" style="vertical-align:top;">
                                    <h4>SHIP TO:</h4>
                                    <small>
                                    <strong><?php echo $tmpAddr[0]['address_fullname']; ?></strong><br />
                                    <?php echo $tmpAddr[0]['address_line1']; ?><br />
                                    <?php if ($tmpAddr[0]['address_line2'] != "") { ?>
                                    <?php echo $tmpAddr[0]['address_line2']; ?><br />
                                    <?php }?>
                                    <?php echo $tmpAddr[0]['address_city']; ?> <?php echo $tmpAddr[0]['address_region']; ?><br />
                                    <?php echo $tmpAddr[0]['address_country']; ?><br />
                                    <a title="Postcode">#</a>: <?php echo $tmpAddr[0]['address_postcode']; ?><br />
                                    <a title="Telephone">T</a>: <?php echo $tmpAddr[0]['address_phone']; ?><br />
                                    </small>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td>
                            <table align="center" cellpadding="5" cellspacing="0" width="520" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;">
                              <tbody>
                                <tr>
                                  <td width="300" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#f2f2f2;"><small><strong>Product</strong></small></td>
                                  <td width="20" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#f2f2f2;"><small><strong>#</strong></small></td>
                                  <td width="100" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#f2f2f2;"><small><strong>Unit Price</strong></small></td>
                                  <td width="100" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#f2f2f2;"><small><strong>Price</strong></small></td>
                                </tr>
<?php
        foreach($tmpProd as $value) {
          $prodsPrice[$value['product_id']] = $prodIdsCount[$value['product_id']] * $value['product_price'];
?>
                                <tr>
                                  <td width="400" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;background-color:#ffffff;"><small><?php echo $value['product_name']; ?></small></td>
                                  <td width="20" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;background-color:#ffffff;"><small><?php echo $prodIdsCount[$value['product_id']]; ?></small></td>
                                  <td width="50" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;background-color:#ffffff;"><small><?php printf("%01.2f", $value['product_price']); ?></small></td>
                                  <td width="50" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;background-color:#ffffff;"><small><?php printf("%01.2f", $prodsPrice[$value['product_id']]); ?></small></td>
                                </tr>
<?php
        }
        if (isset($prodsPrice)) {
          $totPrice = array_sum($prodsPrice);
        } else {
          $totPrice = 0;
        }
?>
                                 <tr>
                                  <td width="470" colspan="3" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;background-color:#ffffff;"><small>Postage to <?php echo $tmpPostg[0]['postage_country_name']; ?> (by Pos Malaysia)</small></td>
                                  <td width="50" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;background-color:#ffffff;"><small><?php printf("%01.2f", $tmpPostg[0]['postage_country_price']); ?></small></td>
                                </tr>
                                 <tr>
                                  <td width="470" colspan="3" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;background-color:#ffffff;"><small><strong>TOTAL</strong></small></td>
                                  <td width="50" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border-top:1px solid #dddddd;background-color:#ffffff;"><small><strong><?php printf("%01.2f", ($totPrice + $tmpPostg[0]['postage_country_price'])); ?></strong></small></td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td>
                            <table align="center" cellpadding="0" cellspacing="0" width="520" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;">
                              <tbody>
                                <tr>
                                  <td width="100" style="text-align:right;"><small><strong>Status:</strong></small>&nbsp;</td>
                                  <td width="420"><small><?php echo $tmpBill[0]['bill_status']; ?></small></td>
                                </tr>
                                <tr>
                                  <td width="100" style="text-align:right;"><small><strong>Validity:</strong></small>&nbsp;</td>
                                  <td width="420"><small><?php echo seconds_to_days($tmpBill[0]['bill_period']); ?></small></td>
                                </tr>
                                <tr>
                                  <td width="100" style="text-align:right;"><small><strong>Expiry:</strong></small>&nbsp;</td>
                                  <td width="420"><small><?php echo date("Y-m-d H:i:s", ($tmpBill[0]['bill_timestamp'] + $tmpBill[0]['bill_period'])); ?></small> <a title="(UTC+08:00) Kuala Lumpur, Singapore">&otimes;</a></td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <?php if ($tmpBill[0]['bill_status'] == "new") { ?>
                        <tr>
                          <td>
                            <table align="center" cellpadding="0" cellspacing="0" width="520" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;">
                              <tbody>
                                <tr>
                                  <td>
                                    <table align="right" cellpadding="5" cellspacing="2" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;border:1px solid #049cdb;">
                                      <tbody>
                                        <tr>
                                          <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#049cdb;">
                                            <small><a href="<?php echo "http://".$_SERVER['HTTP_HOST']."/partial/admin/transaction.php?".$_SERVER['argv'][0]; ?>" style="text-decoration:none;color:#ffffff;">Make Payment Now &raquo;</a></small>
                                          </td>
                                        </tr>
                                      </tbody>
                                    </table>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                      <?php } ?>
                      </tbody>
                    </table>
                  </td>
                </tr>
                <tr>
                  <td>
                    <table cellpadding="20" cellspacing="0" border="0" width="670" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;background-color:#eeeeee;background-image:url(https://twitter.com/images/email/t1/shadow_bottom.jpg);background-repeat:repeat-x;border-top-color:#dddddd;border-top-style:solid;border-top-width:1px;">
                      <tbody>
                        <tr>
                          <td>
                            <table align="center" cellpadding="0" cellspacing="0" width="520" border="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;">
                              <tbody>
                                <tr>
                                  <td colspan="2" width="520"><small><strong>NOTES:</strong></small></td>
                                </tr>
                                <tr>
                                  <td width="50"><small>1.</small>&nbsp;</td>
                                  <td width="470"><small>Thank you for your order. Please make payment within <?php echo seconds_to_days($tmpBill[0]['bill_period']); ?>.</small></td>
                                </tr>
                                <tr>
                                  <td width="50"><small>2.</small>&nbsp;</td>
                                  <td width="470"><small>Your order will be processed upon payment confirmation.</small></td>
                                </tr>
                                <tr>
                                  <td width="50"><small>3.</small>&nbsp;</td>
                                  <td width="470"><small>You will receive an email from us once your items have been delivered.</small></td>
                                </tr>
                                <tr>
                                  <td width="50"><small>4.</small>&nbsp;</td>
                                  <td width="470"><small>Please whitelist us, <a href="#">xxx.com</a>,from your junk mail filter.</small></td>
                                </tr>
                                <tr>
                                  <td width="50"><small>5.</small>&nbsp;</td>
                                  <td width="470"><small>Please quote your order reference number if you wish to contact us about this order.</small></td>
                                </tr>
                                <tr>
                                  <td colspan="2" width="520"><small>&nbsp;</small></td>
                                </tr>
                                <tr>
                                  <td colspan="2" width="520"><small>
                                  For more information about this order, please refer to:<br />
                                  <a href="#">Order Tracking Page</a><br />
                                  </small></td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                      </tbody>
                    </table>
                  </td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
<?php
      } else {
        $error_msg = "E-mail error.";
        printAlert("error", "Oh snap!", $error_msg, "#");
        printContinueShopping("#");
      }
    } else {
      $error_msg = "Transaction error.";
      printAlert("error", "Oh snap!", $error_msg, "#");
      printContinueShopping("#");
    }
  } else {
    $error_msg = "Page error.";
    printAlert("error", "Oh snap!", $error_msg, "#");
    printContinueShopping("#");
  }
?>
  </body>
</html>